package rsexplorer.debugger;

import rsexplorer.rstree.ArrayFieldTreeNode;
import rsexplorer.rstree.ClassTreeNode;
import rsexplorer.tree.SortableTreeNode;
import rsexplorer.utils.ClassResolver;
import rsexplorer.utils.ClassResolvedListener;
import rsexplorer.utils.FieldFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.lang.reflect.Field;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

public class Debugger extends javax.swing.JFrame implements ClassResolvedListener, FieldFilter {

    private Thread searchThread;
    private ClassResolver classResolver;

    public Debugger(ClassResolver resolver) {
        try {
            this.classResolver = resolver;

            dataTreeRoot = new SortableTreeNode("Loaded classes");

            initComponents();

            loadClasses();

            //dataTreeRoot.add(new ObjectFieldTreeNode("I'm here", this));
            this.classResolver.addResolveListener(this);
        } catch (Exception e) {
            //System.out.println(f);
            e.printStackTrace();
            System.exit(1);
        }
    }

    private void loadClasses() {
        Class<?>[] classes = classResolver.getClasses();
        for (Class<?> cls : classes) {
            if (!ClassResolver.isSystemClass(cls)) {
                ClassTreeNode classNode = new ClassTreeNode(cls, this);

                if (classNode.getChildCount() > 0)
                    dataTreeRoot.add(classNode);
            }
        }
        dataTreeModel.reload();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchOptionGroup = new javax.swing.ButtonGroup();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        searchField = new javax.swing.JTextField();
        isValueSearch = new javax.swing.JRadioButton();
        isNameSearch = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dataTreeModel = new DefaultTreeModel(dataTreeRoot);
        dataTree = new rsexplorer.debugger.DataTree(dataTreeModel);

        setTitle("RuneScape Explorer");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                closeRequest(evt);
            }
        });

        jLabel1.setText("Search: ");

        searchField.setName("searchField"); // NOI18N

        searchOptionGroup.add(isValueSearch);
        isValueSearch.setSelected(true);
        isValueSearch.setText("Search values");
        isValueSearch.setName("isValueSearch"); // NOI18N

        searchOptionGroup.add(isNameSearch);
        isNameSearch.setText("Search field names");
        isNameSearch.setName("isNameSearch"); // NOI18N

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchRequest(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(isValueSearch)
                        .addGap(18, 18, 18)
                        .addComponent(isNameSearch))
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(isValueSearch)
                    .addComponent(jLabel1)
                    .addComponent(isNameSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(dataTree);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(524, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void searchRequest(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchRequest
        if (searchThread != null && searchThread.isAlive()) {
            System.out.println("Your search has not yet finished.");
            return;
        }

        searchThread = new Thread(new Runnable() {

            public void run() {
                dataTreeRoot.removeAllChildren();
                loadClasses();
            }
        });
        searchThread.start();
    }//GEN-LAST:event_searchRequest

    private void closeRequest(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeRequest
        classResolver.removeResolveListener(this);
    }//GEN-LAST:event_closeRequest

    public void classResolved(Class<?> reference) {
        //System.out.println("Loaded " + reference.getName());
        if (!ClassResolver.isSystemClass(reference)) {
            ClassTreeNode ctn = new ClassTreeNode(reference, this);

            if (ctn.getChildCount() > 0) {
                dataTreeRoot.add(ctn);
                dataTreeModel.nodesWereInserted(dataTreeRoot, new int[]{dataTreeRoot.getIndex(ctn)});
            }
        }
    }

    public boolean shouldFilterPrimitive(Object object) {
        try {
            return searchOptionGroup.isSelected(isValueSearch.getModel()) && !object.toString().toLowerCase().startsWith(searchField.getText().toLowerCase());
        } catch (Exception e) {
            return false;
        }
    }

    public boolean shouldFilterArray(Object object) {
        return false;
    }

    public boolean shouldFilterObject(Object object) {
        return false;
    }

    public boolean shouldFilterField(Field field) {
        return searchOptionGroup.isSelected(isNameSearch.getModel()) && !field.toString().contains(searchField.getText());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree dataTree;
    private javax.swing.tree.DefaultTreeModel dataTreeModel;
    private SortableTreeNode dataTreeRoot;
    private javax.swing.JRadioButton isNameSearch;
    private javax.swing.JRadioButton isValueSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField searchField;
    private javax.swing.ButtonGroup searchOptionGroup;
    // End of variables declaration//GEN-END:variables
}
